
// 图片，视频，音频的回调函数全部类似
import callback from './media-callback'
export default {
  name: 'xblFileUploadFinish',
  media: {
    name: 'xblSelectFinish',
    callback: callback
  },
  handler (option = {}, jsonStr, uploadType, type) {
    // 0是上传成功了
    if (uploadType === '0' && (type === 'video' || type === undefined)) {
      let fileInfo = JSON.parse(jsonStr)
      return {
        filename: fileInfo.fileName,
        fromNative: true,
        size: fileInfo.size,
        url: fileInfo.localPath,
        ext: fileInfo.format,
        type: 'video',
        data: fileInfo
      }
    } else if (type === 'image') {
      let res; let ext = 'png'; let filename
      let uuidName = '94662500-c761-11ec-8672-a9f184b6b993'
      if (typeof window.$ui.uuid === 'function') {
        uuidName = window.$ui.uuid()
      } else if (typeof window.$ui.uuid === 'object') {
        if (window.$ui.uuid.hasOwnProperty('default')) {
          uuidName = window.$ui.uuid.default()
        } else if (window.$ui.uuid.hasOwnProperty('v1')) {
          uuidName = window.$ui.uuid.v1()
        }
      }
      // let uuidName = typeof window.$ui.uuid === 'function' ? window.$ui.uuid() : window.$ui.uuid.default()
      filename = (option.filename || uuidName) + '.png'
      let fileType = type
      let base64Data = JSON.parse(jsonStr)
      let mimeType = `${type}/${ext}`
      if (Array.isArray(base64Data)) {
        res = []
        base64Data.forEach((b64, idx) => {
          let blobAndUrl = window.$ui.base64.toBlobAndUrl(b64, mimeType)
          res.push({
            ext,
            mimeType,
            type: uploadType || type,
            fileType: type,
            url: blobAndUrl.url,
            fromNative: true,
            filename: filename,
            data: blobAndUrl.blob,
            size: blobAndUrl.blob.size
          })
        })
      } else {
        let blobAndUrl = window.$ui.base64.toBlobAndUrl(base64Data, mimeType)
        res = {
          ext,
          mimeType,
          type: type,
          fileType: fileType,
          url: blobAndUrl.url,
          fromNative: true,
          isRecording: true,
          filename: filename,
          data: blobAndUrl.blob,
          size: blobAndUrl.blob.size
        }
      }
      return {
        data: res,
        type: uploadType || type
      }
    }
  }
}
